<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8" />
	<title>书店管理系统</title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
	<link rel="stylesheet" type="text/css" href="/public/css/bootstrap.min.css" />
	<link rel="stylesheet" type="text/css" href="/public/css/swiper.min.css" />
	<link href="http://cdn.bootcss.com/animate.css/3.5.2/animate.min.css" rel="stylesheet" media="all">
	<link href="/public/css/bootstrap-touch-slider.css" rel="stylesheet" media="all">
	<link rel="stylesheet" type="text/css" href="/public/css/pc.css" />
	<link rel="stylesheet" type="text/css" media="screen and (max-width:1200px)" href="/public/css/ipad.css">
	<link rel="stylesheet" type="text/css" media="screen and (max-width:768px)" href="/public/css/phone.css">
	<script type="text/javascript" src="/public/js/jquery-1.11.1.min.js"></script>
	<script type="text/javascript" src="/public/js/bootstrap.min.js"></script>
	<script type="text/javascript" src="/public/js/swiper.min.js"></script>
	<script type="text/javascript" src="/public/js/scrollReveal.js"></script>
	<script src="http://cdn.bootcss.com/jquery.touchswipe/1.6.18/jquery.touchSwipe.min.js"></script>
	<script src="/public/js/bootstrap-touch-slider-min.js"></script>
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=Diy1GpUowSnG4kyw93KWvG4GfQDm3xch"></script>

	<!-- 地区插件 -->
	<script src="/public/js/distpicker.data.js"></script>	
	<script src="/public/js/distpicker.js"></script>
	<script src="/public/js/main.js"></script>	

	<script type="text/javascript" src="/public/js/jquery.cookie.js"></script>
	<script type="text/javascript" src="/public/js/base64.js"></script>
	<script type="text/javascript" src="/public/js/xiuzhen.js"></script>
	<script>
		if ($(document.body).width() < 760) {
			document.documentElement.style.fontSize = document.documentElement.clientWidth / 7.5 + 'px';
		} else {
			document.documentElement.style.fontSize = document.documentElement.clientWidth / 1 + 'px';
		}
	</script>
	<!--兼容ie8-->
	<!--[if lt IE 9]>
	    <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
	    <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
	<!-- 头部导航开始 -->
	<div class="header">
		<div class="nav">
			<img class="logo" src="/public/images/logo.png" alt="">
			<ul class="top">
				<li>
					<a href="index.html">首页</a>
				</li>
				<li>
					<a href="box.html">图书展示</a>
				</li>
				<li>
					<a href="labOne.html">图书资讯</a>
					
				</li>
				
				<li>
					<a href="about.html">关于我们</a>
				</li>
				<li>
					<a href="contact.html">联系我们</a>
				</li>
			</ul>
			<div class="right">
				<a href="login.html" class="login">会员登陆</a>
				
				<a class="indexCart" href="cart.html">
					<img class="cart" src="/public/images/cart.png" alt="">
				</a>
				<a class="leaguer" href="member.html" style="display: none;">
					<img class="" src="/public/images/people2.png" alt="">
				</a>
				<p id="username" style="display: none;"></p>
				<!-- <p class="line"></p>
				<img class="weixin" src="images/weixin.png" alt=""> -->
			</div>
		</div>
	</div>
	
	<!-- 购物车页面 -->
	<div class="cart">
		<div class="container">
			<p class="title">我的购物车</p>
			<div class="top">
				<div class="check"></div>
				<div class="info">商品信息</div>
				<div class="price">单价（元）</div>
				<div class="num">数量</div>
				<div class="total">总价 ( 元 )</div>
				<div class="total">类型</div>
				<div class="operate">操作</div>
			</div>
			<div class="content">
				
				<!-- <div class="item">
					<div class="check">
						<input type="checkbox" value="">
					</div>
					<div class="info">
						<img src="images/boxList1.png" alt="">
						<p class=""></p>
					</div>
					<div class="price">
						<span>259.00</span>元
					</div>
					<div class="num">
						<span class="opp add">+</span>
						<span class="number">1</span>
						<span class="opp count">-</span>
					</div>
					<div class="total">
						<span>259.00</span>元
					</div>
					<div class="operate">
						<a href="">删除订单</a>
					</div>
				</div> -->
			</div>
			<div class="bottom">
				<div class="all">
					<label>
						<input type="checkbox"> 全选
					</label>
					<a href="javascript:;">批量删除</a>
				</div>
				<div class="amount">
					<p>总计</p>
					<p><span>0.00</span>元</p>
				</div>
				<div class="consult">
					<p>已选商品<span class="totalQuantity">0</span>件</p>
					<p>合计（不含运费）<span class="totalPrice">0.00</span>元</p>
				</div>
			</div>
			<div class="place">
				<a href="index.html">去逛逛</a>
				<a href="javascript:;" class="account">去结算</a>
			</div>
		</div>
	</div>

	<!-- 预约弹框样式 -->
	<div class="appointShadow"></div>

	<!-- 右侧弹框 -->
	<div class="rightFrame">
		<div class="bespoke"><a href="saleApply.html">代售申请</a></div>
		<div class="placement"><img src="/public/images/top.png" alt="">置顶</div>
	</div>

	<!-- 弹框样式 -->
	<div class="alert-box">
		<span class="xz-alert alert alert-danger"></span>
	</div>

	<!-- 公共底部开始 -->
	<div class="footer">
		<div class="footer-content1">
			<div></div>
		</div>
		<div class="footer-content">
			<div class="container">
				<div class="content">
					<div class="left">
						<img class="bottom-logo" src="/public/images/logo.png" alt="">
						<p class="des">此系统为不仅为人们提供了便利的购书平台，还在此基础上增加了代售和出租的功能，人们有想卖的书籍可以在此平台申请，平台会帮忙代售，还有的宝宝们，不想购买书籍，此平台提供了租书的功能，不需要购买就可以便宜看到自己想看的书籍啦！！！</p>
					</div>
					<div class="right">
						<div class="contact">
							<p class="title">联系我们</p>
							<p class="phone">电话：0755-23324516</p>
							<p class="email">邮箱：cd.01@oil-com.com</p>
							<p class="email">QQ：1056598469</p>
						</div>
					</div>
				</div>
				<p class="copy">地址：山西省太原市万柏林区下元街道公园时代城2号楼1301</p><br>
			</div>
		</div>	
		
	</div>

	<script type="text/javascript">
	

		if(!$.cookie('uid')){
			location.href = './index.html';
		}
		
		var loginUrl = Base64.encode("cart.html");  
		$('.header .nav .right .login').attr('href','./login.html?loginurl='+loginUrl); 

		// 首页banner
		$('#bootstrap-touch-slider').bsTouchSlider();
		
		// 点击返回顶部
		$('.rightFrame .placement').on('click', function () {
			$("html,body").animate({
				scrollTop: 0
			}, 1000);
		});

		
		$.cookie('type', "",{path: '/'});
		var totalQuantity = 0;   //总数量
		var totalPrice = 0;  	//总价格
		var currentQuantity = 0;  //当前数量
		var currentPrice = 0; 	//当前价格
		var commodityList = []; //要提交的商品数据

		function jisuan(){
			currentPrice = 0;
			currentQuantity = 0;
			$('.cart .content  input[type="checkbox"]:checked').each(function () {
				currentQuantity += parseFloat($(this).attr("data-amounts"));
				currentPrice += parseFloat($(this).attr("data-subtotal"));
			});
			$(".cart .bottom .amount p:nth-child(2) span").html(currentPrice);
			$(".cart .bottom .consult p span.totalQuantity").html(currentQuantity);
			$(".cart .bottom .consult p span.totalPrice").html(currentPrice);

			var allNumber = $(".cart .content input[type=checkbox]").length;
			var checkNumber = $(".cart .content input:checked").length;
			if( allNumber>0 && allNumber==checkNumber){
				$(".all input:nth-child(1)").prop("checked",true) 
			}else{
				$(".all input:nth-child(1)").prop("checked",false) 
			}
		}

		cartData();
		function cartData(){
			$.ajax({
                type: "get",
                dataType: "", // submit info
                url: url + '/v1/front/mall/cart?userId='+ $.cookie('uid'),
                success: function (res) {
                    if (!res.resultStatus) {
                        $(".xz-alert").html(res.errorMessage);
                        $('.xz-alert').show();
                        setTimeout(function () {
                            $(".xz-alert").hide();
                        }, 3000);
                        return
					}
					var cart = "";   //购物车商品
					var cartArr = res.resultData;
                    for (var i = 0; i < cartArr.length; i++) {
						cart += '<div class="item">' +
								'<div class="check">'+
								'<input type="checkbox" value="" data-cartid="'+ cartArr[i].cartId +'" data-amounts="'+ cartArr[i].amounts +'" data-commodityid="'+ cartArr[i].commodityId +'" data-commodityname="'+ cartArr[i].caption +'" data-type="'+ cartArr[i].type +'" data-subtotal="'+ cartArr[i].amounts*cartArr[i].price +'" data-simple="'+ cartArr[i].bgPicture +'" data-minprice="'+ cartArr[i].price +'">'+
								'</div>'+
								'<div class="info">'+
								'<img src="'+ cartArr[i].bgPicture +'" alt="">'+
								'<p class="">'+ cartArr[i].caption +'</p>'+
								'</div>'+
								'<div class="price">'+
								'<span>'+ cartArr[i].price +'</span>元'+
								'</div>'+
								'<div class="num">'+
								'<span class="opp add" onclick="numChange(1,'+ cartArr[i].amounts +','+ cartArr[i].cartId +')">+</span>'+
								'<span class="number">'+ cartArr[i].amounts +'</span>'+
								'<span class="opp count" onclick="numChange(2,'+ cartArr[i].amounts +','+ cartArr[i].cartId +')">-</span>'+
								'</div>'+
								'<div class="total">'+
								'<span>'+ cartArr[i].amounts*cartArr[i].price +'</span>元'+
								'</div>'+
								'<div class="total">'+
								''+ cartArr[i].type +''+
								'</div>'+
								'<div class="operate">'+
								'<a href="javascript:;" class="delete" onclick="deleCart('+ cartArr[i].cartId +')">删除商品</a>'+
								'</div></div>'	
						totalQuantity += parseInt(cartArr[i].amounts);
						totalPrice += cartArr[i].amounts*cartArr[i].price/100;
					}

					$(".cart .content").html(cart);

					
					// 全选和商品选择
					$(".cart .content input[type=checkbox]").click(function(){
						currentQuantity = $(".cart .bottom .consult p span.totalQuantity").html();
						currentPrice = $(".cart .bottom .consult p span.totalPrice").html();
						if(!$(this).prop("checked") == true){
							$(".all input:nth-child(1)").prop("checked",false) 
							var _thisQuantity = $(this).attr("data-amounts");
							var _thisPrice = $(this).attr("data-subtotal");
							currentQuantity = parseFloat(currentQuantity) - parseFloat(_thisQuantity);
							currentPrice = parseFloat(currentPrice) - parseFloat(_thisPrice);
						}else{
							var allNumber = $(".cart .content input[type=checkbox]").length;
							var checkNumber = $(".cart .content input:checked").length;
							if(allNumber==checkNumber){
								$(".all input:nth-child(1)").prop("checked",true) 
							}
							var _thisQuantity = $(this).attr("data-amounts");
							var _thisPrice = $(this).attr("data-subtotal");
							currentQuantity = parseFloat(currentQuantity) + parseFloat(_thisQuantity);
							currentPrice = parseFloat(currentPrice) + parseFloat(_thisPrice);
						}
						$(".cart .bottom .amount p:nth-child(2) span").html(currentPrice);
						$(".cart .bottom .consult p span.totalQuantity").html(currentQuantity);
						$(".cart .bottom .consult p span.totalPrice").html(currentPrice);
					})

					jisuan();
                },
                error: function (e) {
                    $(".xz-alert").html("请求数据失败");
                    $('.xz-alert').show();
                    setTimeout(function () {
                        $(".xz-alert").hide();
                    }, 3000);
                }
			})	
		}

		// 删除购物车	
		function deleCart(id){
			$.ajax({
                type: "DELETE",
                dataType: "", // submit info
                url: url + '/v1/front/mall/cart/'+ id,
                success: function (res) {
                    if (!res.resultStatus) {
                        $(".xz-alert").html(res.errorMessage);
                        $('.xz-alert').show();
                        setTimeout(function () {
                            $(".xz-alert").hide();
                        }, 3000);
                        return
					}
					$(".xz-alert").html("删除成功！");
                    $('.xz-alert').show();
                    setTimeout(function () {
						$(".xz-alert").hide();
						cartData();
					}, 1000);
					
                },
                error: function (e) {
                    $(".xz-alert").html("请求数据失败");
                    $('.xz-alert').show();
                    setTimeout(function () {
                        $(".xz-alert").hide();
                    }, 3000);
                }
			})
		}

		// 购物车的加减
		function numChange(type,num,cartid){
			var number = '';
			if(type=='2'){
				if(num <= 1){
					$(".xz-alert").html("数量不能小于1哦！");
					$('.xz-alert').show();
					setTimeout(function () {
						$(".xz-alert").hide();
					}, 1000);
				}else{
					number = parseInt(num) - 1;
				}
				
			}else if(type=='1'){
				number = parseInt(num) + 1;
			}
			if(number>=1){
				$.ajax({
					type: "PUT",
					dataType: "", // submit info
					url: url + '/v1/front/mall/cart/'+ cartid + '?amounts=' + number,
					success: function (res) {
						if (!res.resultStatus) {
							$(".xz-alert").html(res.errorMessage);
							$('.xz-alert').show();
							setTimeout(function () {
								$(".xz-alert").hide();
							}, 3000);
							return
						}
						cartData();
						
					},
					error: function (e) {
						$(".xz-alert").html("修改数据失败");
						$('.xz-alert').show();
						setTimeout(function () {
							$(".xz-alert").hide();
						}, 3000);
					}
				})
			}
			
		}

		// 全选
		$(".all input:nth-child(1)").click(function(){
			if($(this).prop("checked") == true){ 
				$(".cart .content input[type=checkbox]").prop("checked",true);
				jisuan();
			}
		})

		// 去结算
		$(".cart .account").click(function(){
			if($('.cart .content  input[type="checkbox"]:checked').length==0){
				$(".xz-alert").html("请先选择要买的商品！");
				$('.xz-alert').show();
				setTimeout(function () {
					$(".xz-alert").hide();
				}, 2000);
			}else{
				$('.cart .content  input[type="checkbox"]:checked').each(function () {
					var commodity = {};
					commodity.commodityId = $(this).attr("data-commodityid");
					commodity.amounts = $(this).attr("data-amounts");
					commodity.price = $(this).attr("data-subtotal");
					commodity.nowPrice = $(this).attr("data-minprice");
					commodity.simple = $(this).attr("data-simple");
					commodity.caption = $(this).attr("data-commodityname");
					commodity.type = $(this).attr("data-type");
					commodity.cartId = $(this).attr("data-cartid");
					commodityList.push(commodity);
				});
				window.localStorage.commodityList = JSON.stringify(commodityList);
				location.href = './confirm.html?cartTigs=1&type=111';
			}
			
		})


		// 批量删除
		var idArr = [];
		$(".cart .bottom .all a").click(function(){
			if($('.cart .content  input[type="checkbox"]:checked').length==0){
				$(".xz-alert").html("请先选择要删除的商品！");
				$('.xz-alert').show();
				setTimeout(function () {
					$(".xz-alert").hide();
				}, 2000);
			}else{
				$('.cart .content  input[type="checkbox"]:checked').each(function () {
					var cartidItem = commodityIdItem =  $(this).attr("data-cartid");
					idArr.push(cartidItem);
				});
				var idString = idArr.join(',');
				$.ajax({
					type: "POST",
					dataType: "", // submit info
					url: url + '/v1/front/mall/cart/moreDestroy?cartId='+ idString,
					success: function (res) {
						if (!res.resultStatus) {
							$(".xz-alert").html(res.errorMessage);
							$('.xz-alert').show();
							setTimeout(function () {
								$(".xz-alert").hide();
							}, 3000);
							return
						}
						$(".xz-alert").html("删除成功！");
						$('.xz-alert').show();
						setTimeout(function () {
							$(".xz-alert").hide();
							cartData();
		
						}, 1000);

						
					},
					error: function (e) {
						$(".xz-alert").html("删除数据失败");
						$('.xz-alert').show();
						setTimeout(function () {
							$(".xz-alert").hide();
						}, 3000);
					}
				})
			}
			
		})
		
	</script>		

</body>

</html>